package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Interface.MasterFace;
import com.example.neusoft.yuejian_12.JavaBean.Master;
import com.example.neusoft.yuejian_12.Listener.MasterCollectListener;
import com.example.neusoft.yuejian_12.Listener.MasterDetailListener;
import com.example.neusoft.yuejian_12.Listener.MasterListener;
import com.example.neusoft.yuejian_12.Listener.MasterQuestionListener;
import com.example.neusoft.yuejian_12.Listener.MasterStateListener;
import com.example.neusoft.yuejian_12.Service.MasterService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by admin on 2017/9/30.
 */

public class MasterModel implements MasterFace {

    private Retrofit retrofit;
    private MasterService masterService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址*/

    public MasterModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取大师列表
    @Override
    public void getList(String id, String realname,final MasterListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<List<Master>> call =masterService.getmasterlist();
        call.enqueue(new Callback<List<Master>>() {
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<Master>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //获取大师详情
    @Override
    public void getdetail(String id,final MasterDetailListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<Master> call =masterService.getmasterinfo(id);
        call.enqueue(new Callback<Master>() {
            @Override
            public void onResponse(Call<Master> call, Response<Master> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Master> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    //获取大师视频
    @Override
    public void getvideo(String id,final MasterDetailListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<Master> call =masterService.getVideoList(id,"mvideo");
        call.enqueue(new Callback<Master>() {
            @Override
            public void onResponse(Call<Master> call, Response<Master> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<Master> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });

    }
  @Override
    public void insertComment(String master_id, String userid, String question,final MasterQuestionListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<Integer> call =masterService.insertComment(master_id,userid,question);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }



//收藏大师
    @Override
    public void colmaster(String userid, String fan_id,final MasterCollectListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<Integer> call =masterService.colmaster("lgj98rfkiqvenhrk2o7oe2po81",userid,fan_id,"2");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    //大师状态
    @Override
    public void getState(String fan_id, String userid,final MasterStateListener listListener) {
        masterService = retrofit.create(MasterService.class);
        Call<Integer> call =masterService.getState(fan_id,userid,"2","lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    listListener.onResponse(response.body());
                }else{
                    listListener.onFail("fail");
                }

            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

}


   /*public retrofit2.Call<Integer> insertComment(String master_id, int userid, String question){
       masterService = retrofit.create(MasterService.class);
       return  masterService.insertComment(master_id,userid,question);
   }*/




